#!/bin/bash

job_configure_users()
{
	msg_job_start "job_configure_users"

	msg "setup the groups"
	mount -v -t proc none ${mountpoint}/proc 
	mount -v -t sysfs none ${mountpoint}/sys 
	mount -v -o bind /dev ${mountpoint}/dev 
	chroot ${mountpoint} /usr/bin/gpasswd -a ${user_name} users
	chroot ${mountpoint} /usr/bin/gpasswd -a ${user_name} audio
	chroot ${mountpoint} /usr/bin/gpasswd -a ${user_name} video
	chroot ${mountpoint} /usr/bin/gpasswd -a ${user_name} network
	chroot ${mountpoint} /usr/bin/gpasswd -a ${user_name} optical
	chroot ${mountpoint} /usr/bin/gpasswd -a ${user_name} floppy
	chroot ${mountpoint} /usr/bin/gpasswd -a ${user_name} lp
	chroot ${mountpoint} /usr/bin/gpasswd -a ${user_name} log
	chroot ${mountpoint} /usr/bin/gpasswd -a ${user_name} power
	chroot ${mountpoint} /usr/bin/gpasswd -a ${user_name} camera
	chroot ${mountpoint} /usr/bin/gpasswd -a ${user_name} scanner
	chroot ${mountpoint} /usr/bin/gpasswd -a ${user_name} storage
	chroot ${mountpoint} /usr/bin/gpasswd -a ${user_name} locate

	# add dirs
	chroot ${mountpoint} /bin/mkdir -p -v /home/${user_name}/Desktop

	# add basic configs
	cp -v /etc/skel/.bashrc ${mountpoint}/home/${user_name}/.bashrc
	cp -v /etc/skel/.bash_profile ${mountpoint}/home/${user_name}/.bash_profile
	cp -v /etc/skel/.xinitrc ${mountpoint}/home/${user_name}/.xinitrc
	cp -v /etc/skel/.xsession ${mountpoint}/home/${user_name}/.xsession

	# gtk config
	chroot ${mountpoint} /bin/mkdir -p -v /home/${user_name}/.kde4/env
	cp -v /etc/skel/.gtkrc-2.0-kde4 ${mountpoint}/home/${user_name}/.gtkrc-2.0-kde4
	echo "export GTK2_RC_FILES=/home/${user_name}/.gtkrc-2.0-kde4" > ${mountpoint}/home/${user_name}/.kde4/env/gtk-qt-engine.rc.sh
	chmod a+x ${mountpoint}/home/${user_name}/.kde4/env/gtk-qt-engine.rc.sh

	# chakra settings
	chroot ${mountpoint} /bin/mkdir -p -v /home/${user_name}/.kde4/share/config
	cp -v /etc/skel/ksplashrc ${mountpoint}/home/${user_name}/.kde4/share/config/ksplashrc
	cp -v /etc/skel/plasma-desktop-appletsrc ${mountpoint}/home/${user_name}/.kde4/share/config/plasma-desktop-appletsrc
	cp -v /etc/skel/kcmfonts ${mountpoint}/home/${user_name}/.kde4/share/config/kcmfonts
	cp -v /etc/skel/bookmarks.xml ${mountpoint}/home/${user_name}/.kde4/share/apps/konqueror/bookmarks.xml
	cp -v /etc/skel/rekonqrc ${mountpoint}/home/${user_name}/.kde4/share/config/rekonqrc
	cp -v /etc/skel/khotkeysrc ${mountpoint}/home/${user_name}/.kde4/share/config/khotkeysrc
	cp -v /etc/skel/kglobalshortcutsrc ${mountpoint}/home/${user_name}/.kde4/share/config/kglobalshortcutsrc
        cp -v /etc/skel/yakuakerc ${mountpoint}/home/${user_name}/.kde4/share/config/yakuakerc
        cp -v /etc/skel/kickoffrc ${mountpoint}/home/${user_name}/.kde4/share/config/kickoffrc
	cp -v /etc/skel/networkmanagementrc ${mountpoint}/home/${user_name}/.kde4/share/config/networkmanagementrc

	# white mouse-cursor
	cp -v /etc/skel/kcminputrc ${mountpoint}/home/${user_name}/.kde4/share/config/kcminputrc
	cp -v /etc/skel/kdeglobals ${mountpoint}/home/${user_name}/.kde4/share/config/kdeglobals

	# fixing background
	echo "Search for your current screen resolution (this may flicker)"
	CURRENT_RES=`xrandr | grep '*' | cut -b -16 | sed 's/ //g'`
	echo "Your current screen resolution is: $CURRENT_RES"
	echo "Try to link right backround.jpg"

	FILE="/usr/share/wallpapers/ashoc/contents/images/$CURRENT_RES.jpg"

	if [ -f $FILE ];
	then
	  echo "Linking file $FILE"
	  chroot ${mountpoint} ln -sf $FILE /usr/share/apps/kdm/themes/ashoc/background.jpg
	else
	echo "File $FILE does not exists! Linking 1280x800.jpg"
	  CURRENT_RES="1280x800"
	  chroot ${mountpoint} ln -sf $FILE /usr/share/apps/kdm/themes/ashoc/background.jpg
	fi

	# just for safety, chown the users home and added files
	chroot ${mountpoint} /bin/chown -v ${user_name}:users /home/${user_name}

	chroot ${mountpoint} /bin/chown -v -R ${user_name}:users /home/${user_name}/Desktop

	chroot ${mountpoint} /bin/chown -v ${user_name}:users /home/${user_name}/.bashrc
	chroot ${mountpoint} /bin/chown -v ${user_name}:users /home/${user_name}/.bash_profile
	chroot ${mountpoint} /bin/chown -v -R ${user_name}:users /home/${user_name}/.config
	chroot ${mountpoint} /bin/chown -v ${user_name}:users /home/${user_name}/.gtkrc-2.0-kde4
	chroot ${mountpoint} /bin/chown -v ${user_name}:users /home/${user_name}/.xinitrc
	chroot ${mountpoint} /bin/chown -v ${user_name}:users /home/${user_name}/.xsession
	chroot ${mountpoint} /bin/chown -v -R ${user_name}:users /home/${user_name}/.kde4

	umount -v ${mountpoint}/proc ${mountpoint}/sys ${mountpoint}/dev 

	msg_job_done
}
